<?php
    class test_controller extends Controller
    {
        public function __construct(){
            parent::__construct();
            $this->load->library('FusionCharts');
        }

        public function index(){
        	$this->load->helper('html_helper');
            $this->load->view('test')->display();
		}
        
        public function model(){
        	$this->_testExecutor(SYS_MODEL, 'model');
        }
        
        public function controller(){
        	$this->_testExecutor(SYS_CONTROLLER, 'controller');
        }
        
        public function library(){
        	$this->_testExecutor(SYS_LIB, 'library');
        }
        
        public function helper(){
        	$this->_testExecutor(SYS_HELPER, 'helper');
        }
		
		public function ajax(){
			if (empty($_POST['ele'])){
				$_POST['ele'] = 0;
			}
			else{
				$_POST['ele'] = intval($_POST['ele']);
			}
			
			print $this->load->model('task')->generateChild($_POST['ele']);
		}
        
        private function _testExecutor($dir, $type){
            $inv = false;
            $suffixLen = 0;
            
            switch($type){
            	case 'controller':
                case 'model':
                case 'helper':
              		$fileRegex = '|\.php|iu';
                    $inv = false;
                    $suffixLen = -4;
                    break;
                
                case 'library':
                	$fileRegex = '/^\.|system|error|dispatcher|db|lang|^$|\.php$/ium';
                	$inv = true;
                    $suffixLen = 0;
                    break;
                    
                default: 
                	throw new Exception('Test Component type not correct!');
                	return false;
            }
           
           	$d = opendir($dir);
           	
            while($file = readdir($d)){
            	if (!!preg_match($fileRegex, $file) ^ $inv){
                	print "<p>Now Testing&nbsp;" . basename($file) . "</p>";
                	$this->load->{$type}(0 == $suffixLen ? basename($file) 
                    	: substr(basename($file), 0, $suffixLen));
                }
          	}
            
            print "<p>Complete! All gone!</p>";
            closedir($d);
        }
    }